//
//  HWTweetCommentCell.m
//  HomeWork
//
//  Created by apple on 2017/10/25.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HWTweetCommentCell.h"
#import "HWTweetsModel.h"
@interface HWTweetCommentCell()
/** 姓名 */
@property (strong, nonatomic) UILabel *nameL;
/** 评论内容 */
@property (strong, nonatomic) UILabel *contentL;
@end

@implementation HWTweetCommentCell


+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *identifier = @"HWTweetCommentCell";
    HWTweetCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[HWTweetCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupSubviews];
        self.backgroundColor = UIColorFromRGB(0xdadada);
    }
    return self;
}

- (void)setupSubviews{
    
    UILabel *nameL = [[UILabel alloc] init];
    self.nameL = nameL;
    [self addSubview:nameL];
    nameL.chainFont(FONT(15)).chainTextColor([UIColor blueColor]);
    
    UILabel *contentL = [[UILabel alloc] init];
    self.contentL = contentL;
    [self addSubview:contentL];
    contentL.chainFont(FONT(15)).chainLines(0).chainTextColor(Color333333);
}

- (void)layoutUI{
    
    [self.nameL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView);
        make.top.mas_equalTo(homeScale(3));
    }];
    [self.contentL mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameL.mas_right).offset(3);
        make.right.mas_equalTo(self.contentView);
        make.top.mas_equalTo(self.nameL);
    }];
}


- (void)setComment:(HWTweetCommentUser *)comment{
    _comment = comment;
    self.nameL.text = [NSString stringWithFormat:@"%@:", comment.sender.username];
    self.contentL.text = comment.content;
    [self layoutUI];
}

+ (CGFloat)heightWithModel:(HWTweetCommentUser *)model{
    if (!model.rowHeight) {
        HWTweetCommentCell *cell = [[HWTweetCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@""];
        cell.comment = model;
        
        [cell layoutIfNeeded];
        
        CGRect frame = cell.contentL.frame;
        model.rowHeight = frame.origin.y + frame.size.height + homeScale(3);
        
        return model.rowHeight;
    } else {
        return model.rowHeight;
    }
}

@end
